package cn.enjoy.rabbitmq.handler.original;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import lombok.SneakyThrows;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * @author K
 * @date 2020/11/26 10:26
 */
public class FanoutProducer {

    //交换机名称
    static final String FANOUT_EXCHAGE = "fanout_exchange";
    //队列名称
    static final String FANOUT_QUEUE_1 = "fanout_queue_1";
    //队列名称
    static final String FANOUT_QUEUE_2 = "fanout_queue_2";

    @SneakyThrows
    public static void main(String[] args) {
        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare(FANOUT_EXCHAGE, BuiltinExchangeType.FANOUT);

        channel.queueDeclare(FANOUT_QUEUE_1, true, false, false, null);
        channel.queueDeclare(FANOUT_QUEUE_2, true, false, false, null);

        channel.queueBind(FANOUT_QUEUE_1, FANOUT_EXCHAGE, "");
        channel.queueBind(FANOUT_QUEUE_2, FANOUT_EXCHAGE, "");

        for (int i = 1; i <= 10; i++) {
//            String message = "你好；小兔子！发布订阅模式--" + i;
            ArrayList<String> strList = new ArrayList<>(Arrays.asList("1", "2", "3"));
            ObjectMapper mapper = new ObjectMapper();
            String s = mapper.writeValueAsString(strList);
            channel.basicPublish(FANOUT_EXCHAGE, "", null, s.getBytes());
//            System.out.println("已发送消息：" + message);
        }

        ConnectionUtil.closeAll(channel, connection);
    }
}
